package org.yray.sip.creator;



import org.yray.sip.base.MsgEle;
import org.yray.sip.server.SipSession;

import java.util.Optional;


public class ByeCreator extends MessageCreator {
    /***
     *
     * BYE sip:77001@172.130.13.31:14450;ob SIP/2.0
     Via: SIP/2.0/UDP 172.168.200.78;rport;branch=z9hG4bK2BeNBmgKZrajB
     Max-Forwards: 70
     From: <sip:3308913430565773@172.168.200.78>;tag=4Q3tHNaDBe25Q
     To: "77001" <sip:77001@172.168.200.78>;tag=c569d22682e673588e2e7e446356afa7
     Call-ID: 40ad6521b5534154b7914994230fc3d0
     CSeq: 44264624 BYE
     User-Agent: OE+ PBX
     Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY, PUBLISH, SUBSCRIBE
     Supported: path, replaces
     Reason: Q.850;cause=16;text="NORMAL_CLEARING"
     Content-Length: 0
     * @return
     */
    @Override
    public String createMsg(SipSession session){
        StringBuilder sb = new StringBuilder();
        sb.append("BYE sip:"+session.getTo()+"@"+session.getSipIpPort()+";transport=udp SIP/2.0").append(MsgEle.LINE_SEPARATOR);
        sb.append("Via: SIP/2.0/UDP "+ session.getLocalIpPort()+";rport;branch="+session.getBranch()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Max-Forwards: 70").append(MsgEle.LINE_SEPARATOR);
        Optional.ofNullable(session.getSipMap()).ifPresent(
                map->{
                    Optional.ofNullable(session.getSipMap().get(MsgEle.CALL_ID)).ifPresent(v->{
                        sb.append(MsgEle.CALL_ID+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                    Optional.ofNullable(session.getSipMap().get(MsgEle.FROM)).ifPresent(v->{
                        sb.append(MsgEle.FROM+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                    Optional.ofNullable(session.getSipMap().get(MsgEle.TO)).ifPresent(v->{
                        sb.append(MsgEle.TO+": ").append(v).append(MsgEle.LINE_SEPARATOR);
                    });
                }
        );
        sb.append("CSeq: "+session.getCseq()+" BYE").append(MsgEle.LINE_SEPARATOR);
        sb.append("User-Agent: Robot No1").append(MsgEle.LINE_SEPARATOR);
        sb.append("Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY, PUBLISH, SUBSCRIBE").append(MsgEle.LINE_SEPARATOR);
        sb.append("Supported: path, replaces").append(MsgEle.LINE_SEPARATOR);
        sb.append("Reason: Q.850;cause=16;text=\"NORMAL_CLEARING\"").append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Length:  0").append(MsgEle.LINE_SEPARATOR);
        sb.append(MsgEle.LINE_SEPARATOR);
        return sb.toString();
    }


}
